var uriUtil = require('../../utils/uri.js');
var header = uriUtil.header
Page({
  data: {
    activeIndex:null,
    foods:[],
    sports:[],
    food_sport_data:[],
    show:[],
    meum:['食物摄入','运动消耗'],
    title:'食物摄入',
    record:null,
    calorie:0,
    unit:0,
    now_state:null,
    ds:false,//false为diet；true为sport
    food: {
      uid: 0,
      fid: 0,
      name:'',
      heatin:0,
      day:'',
      kg:0,
      url:''
    },
    sport: {
      uid: 0,
      sid: 0,
      name:'',
      heatout:0,
      day:'',
      time:0,
      url:''
    },
    fsid:0,
  },
  //界面初始化
  onLoad(){
    let foods = []
    let sports = []
    let as=this.data.food_sport_data
    let url1 = uriUtil.BASE_URL+uriUtil.foodList
    let url2 = uriUtil.BASE_URL+uriUtil.sportList
    wx.request({
      url: url1,
      method: 'get',
      success: function (res) {
        foods=res.data
        as.push(foods)
      }
    })
    wx.request({
      url: url2,
      method: 'get',
      success: function (res) {
        sports=res.data
        as.push(sports)
      }
    })
    this.setData({
      food_sport_data:as
    })
    
  },
  //左侧选择
  titleChange(e) {
    let that = this;
    let index= e.currentTarget.dataset.index;
    let showing = that.data.food_sport_data[index];
    let ds = that.data.ds
    if(index==0){
      ds=false
    }else{
      ds=true
    }
    this.setData({
      activeIndex: index,
      title : that.data.meum[index],
      show: showing,
      ds:ds
    })
  },
  //右侧选择
  Popup(e){
    var that = this 
    let index = e.currentTarget.dataset.index
    that.setData({
      now_state:true,
      record:that.data.show[index]
    })
    this.getid()
  },
  //取消选择
  hideModal(e){
    //首先创建一个动画对象（让页面不在是一个“死页面”）
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    //animation.translateY(300)中的translate函数是表示在y轴上平移多少px，而后面紧接着的.step表示前面动画的完成，可以开始下一个动画了
    animation.translateY(300).step()
    this.setData({
      /*这里的export函数是导出动画队列，在外米的wxml中会用到该数据，同时export方法在调用完后会清掉前面的动画操作 */
      animationData: animation.export(),
    })
    /*这里的setTimeout是一个延时器，而它在这里延时了200ms，然后在执行动画 */
    setTimeout(function () {
      animation.translateY(0).step()
      this.setData({
        animationData: animation.export(),
        now_state: false,
      })
    }.bind(this), 20)
  },
  //数据输入
  inputAction(input){
    let value = input.detail.value
    let s = (""+value)
    let split = s.split('.')
    let res = ''
    if(split[0].length>3){
      wx.showToast({
        title: '哥，别吹牛🖊',
        duration: 1000,
        icon:'error',
      })
      split[0]=split[0].substring(0,3)
    }
    res=split[0]
    if(split.length==2){
      if(split[1].length>1){
        wx.showToast({
          title: '小数不能大于1',
          duration: 1000,
          icon:'error',
        })
        split[1]=split[1].substring(0,1)
      }
      res+='.'+split[1]
    }
    var val=parseFloat(res.length==0?"0":res)
    let ds = this.data.ds
    if(!ds){
      val= (val/100*this.data.record.heat).toFixed(1)
    }else{
      val= (val/60*this.data.record.heat).toFixed(1)
    }
  
    this.setData({
      calorie : val,
      unit : value
    }) 
    return res
  },
  //获得单元id
  getid:function(){
    let ds = this.data.ds
    let url= ''
    let name = this.data.record.name
    var that = this
    if(!ds){
      url = uriUtil.BASE_URL+uriUtil.foodid
    }else{
      url = uriUtil.BASE_URL+uriUtil.sportid
    }
    wx.request({
      url: url,
      data: {
        name:name
      },  
      method: 'get',
      success:function(res){
        that.setData({
          fsid:res.data
        })
      },
    })
  },
  //添加记录
  add:function(){
    let url = ''
    let uid = uriUtil.getUserInfo().id
    let day = uriUtil.getDate()
    let ds = this.data.ds
    let name = this.data.record.name
    let url1 = this.data.record.avatarUrl
    let fsid = this.data.fsid
    let that = this
    if(!ds){
      let food = new Object()
      food.uid=uid,
      food.fid=fsid,
      food.name=name,
      food.heatin=this.data.calorie,
      food.day=day,
      food.url= url1,
      food.kg=this.data.unit
      this.setData({
        food:food
      })
      url = uriUtil.BASE_URL+uriUtil.userFoodsAdd;
      wx.request({
        url: url,
        header: header,
        data:food,
        method: 'post',
        success: function (res){
            wx.showToast({
              title: '添加成功',
              duration:500
            })
        }})
        setTimeout(()=>
        {that.hideModal()}, 500)
        this.updaterest(this.data.calorie,0)
      }
      else{
        let sport = new Object()
        sport.uid=uid,
        sport.sid=fsid,
        sport.name=name,
        sport.heatout=this.data.calorie,
        sport.day=day,
        sport.url = url1,
        sport.time=this.data.unit
        this.setData({
          sport:sport
        })
        url = uriUtil.BASE_URL+uriUtil.userSportAdd;
        wx.request({
          url: url,
          header: header,
          data: sport,
          method: 'post',
          success: function (res){
              wx.showToast({
                title: '添加成功',
                duration:500
              })
          }})
          setTimeout(()=>
          {that.hideModal()}, 500)
          this.updaterest(this.data.calorie,1)
      }
    },
  //更新今日摄入
  updaterest:function(calorie,fors){
    wx.request({
      url: uriUtil.BASE_URL+uriUtil.userCalorieup,
      method:"POST",
      header:uriUtil.header,
      data:{
        uid:uriUtil.getUserInfo().id,
        day:uriUtil.getDate(),
        calorie:calorie,
        fors:fors
      },
    })
  }
})